package spider

import (
	"io"
	"log"
	"net/http"
	"os"
)

type Downloader struct {
	Destination string
}

func NewDownloader(dest string) *Downloader {
	return &Downloader{
		Destination: dest,
	}
}

func (d *Downloader) Download(audio Audio) {
	file, err := os.Create(d.Destination + audio.Name + ".m4a")
	if err != nil {
		log.Fatalf("create file err: %v", err)
	}
	defer file.Close()

	resp, err := http.Get(audio.Url)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	io.Copy(file, resp.Body)
}
